#include <stdlib.h>
#include "usart.h"
#include "esp8266.h"
#include <ctype.h>
#include "esp32.h"


static char ble_mac_static_buf[20]={0}; // 17个字符的MAC地址 + 1个null字符 + 2个双引号
static char ble_src_index_buf[1024]={0};
#define BLE_SRC_INDEX_BUF_SIZE 1024

int esp32_ble_init(int x)
{
	char		atcmd[128] = {0x00};

	if(x!=1&&x!=2)
	{
		printf("Fill in parameter 1 as the client and 2 as the server\r\n");
		return -1;
	}
	snprintf(atcmd,sizeof(atcmd),"AT+BLEINIT=%d\r\n",x);
	if(send_atcmd(atcmd,"OK",500))
	{
		printf("error:AT+BLEINIT command failure\r\n");
		return -1;
	}
	printf("make CLIENT=%d successfully!\r\n",x);
	return 0;
}

void parse_ble_mac(const char *response) {
    // 假设MAC地址在响应中的格式是 "BLEADDR:<MAC>\r\n"
    const char *mac_start = strstr(response, "BLEADDR:");
    if (mac_start) {
        mac_start += strlen("BLEADDR:");
        
        strncpy(ble_mac_static_buf, mac_start, sizeof(ble_mac_static_buf) - 1);
        ble_mac_static_buf[strcspn(ble_mac_static_buf, "\r\n")] = 0; 
    } else {
        ble_mac_static_buf[0] = '\0'; 
    }
}

const char* esp32_ble_mac(void) {
    int rv = send_atcmd("AT+BLEADDR?\r\n", EXPECT_OK, 500);
    if (rv != 0) {
       
        ble_mac_static_buf[0] = '\0';
        return ble_mac_static_buf; 
    }

    parse_ble_mac(g_wifi_rxbuf);

    return ble_mac_static_buf;
}
int esp32_ble_gatts_init(void)
{
	if(send_atcmd("AT+BLEGATTSSRVCRE\r\n",EXPECT_OK,500))
	{
		printf("error:AT+BLEGATTSSRVCRE command failure\r\n");
		return -1;
	}
	printf("Gaits init successfully!\r\n");
	return 0;
}
int esp32_ble_gatts_start(void)
{
	if(send_atcmd("AT+BLEGATTSSRVSTART\r\n",EXPECT_OK,500))
	{
		printf("error:AT+BLEGATTSSRVSTART command failure\r\n");
		return -1;
	}
	printf("Gaits start successfully!\r\n");
	return 0;
}

const char* esp32_ble_srv_index(void) {

    int rv = send_atcmd("AT+BLEGATTSCHAR?\r\n", EXPECT_OK, 500);
    if (rv != 0) {
    	ble_src_index_buf[0] = '\0';
        return ble_src_index_buf; 
    }

    parse_ble_srv_index(g_wifi_rxbuf);

    return ble_src_index_buf;
}

void parse_ble_srv_index(const char *response) {
    const char *index_start = response;
    int index_len = 0;
    int buf_len = 0;

    while ((index_start = strstr(index_start, "BLEGATTSCHAR:")) != NULL) {
        index_start += strlen("BLEGATTSCHAR:");

        const char *index_end = strchr(index_start, '\n');
        if (index_end == NULL) {

            index_end = response + strlen(response);
        }


        index_len = index_end - index_start;


        if (buf_len + index_len + 1 > BLE_SRC_INDEX_BUF_SIZE) {

            break;
        }

        strncpy(ble_src_index_buf + buf_len, index_start, index_len);
        ble_src_index_buf[buf_len + index_len] = '\n'; 
        buf_len += index_len + 1; 

        index_start = index_end + 1; 
    }


    ble_src_index_buf[buf_len] = '\0';
}

int esp32_ble_sysmsg(int x)
{
	char		atcmd[128] = {0x00};

	snprintf(atcmd,sizeof(atcmd),"AT+SYSMSG=%d\r\n",x);
	if(send_atcmd(atcmd,"OK",500))
	{
		printf("error:AT+SYSMSG command failure\r\n");
		return -1;
	}
	printf("make SYSMSG=%d successfully!\r\n",x);
	return 0;
}

int esp32_ble_adv(void)
{
	if(send_atcmd("AT+BLEADVSTART\r\n",EXPECT_OK,500))
	{
		printf("error:AT+BLEADVSTART command failure\r\n");
		return -1;
	}
	printf("ADV successfully!\r\n");
	return 0;
}

int esp32_ble_sppcfg(int x1,int x2,int x3,int x4,int x5)
{
	char		atcmd[128] = {0x00};

	/*if(x!=1&&x!=2)
	{
		printf("");
		return -1;
	}*/
	snprintf(atcmd,sizeof(atcmd),"AT+BLESPPCFG=%d,%d,%d,%d,%d\r\n",x1,x2,x3,x4,x5);
	if(send_atcmd(atcmd,"OK",500))
	{
		printf("error:AT+BLESPPCFG command failure\r\n");
		return -1;
	}
	printf("make BLESPPCFG=%d,%d,%d,%d,%d successfully!\r\n",x1,x2,x3,x4,x5);
	return 0;
}
int esp32_ble_spp(void)
{
	if(send_atcmd("AT+BLESPP\r\n",EXPECT_OK,500))
	{
		printf("error:AT+BLESPP command failure\r\n");
		return -1;
	}
	printf("BLE SPP successfully!\r\n");
	return 0;
}

int esp32_ble_send(float data)
{
	char		atcmd[128] = {0x00};
	if(!data||sizeof(data)<=0)
	{
		printf("error:invalid input arguments\r\n");
		return -1;
	}
	snprintf(atcmd,sizeof(atcmd),"ISKBoard send %d bytes to other device by ble:%f\r\n",sizeof(data),data);
	send_atcmd(atcmd, "", 500);
	return sizeof(data);
}


